using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;


namespace Week1ContentExtension
{
    /// <summary>
    /// update material to use custom effect (default.fx)
    /// </summary>
    [ContentProcessor(DisplayName = "Week1ContentExtension.CustomModelProcessor")]
    public class CustomModelProcessor : Microsoft.Xna.Framework.Content.Pipeline.Processors.ModelProcessor
    {
        ContentIdentity oIdentity;

        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            oIdentity = input.Identity;
            return base.Process(input, context);
        }

        protected override MaterialContent ConvertMaterial(MaterialContent material, ContentProcessorContext context)
        {
            // Create a new material.
            EffectMaterialContent newMaterial = new EffectMaterialContent();

            // Tell it to use our custom InstancedModel.fx shader.
            newMaterial.Effect = new ExternalReference<EffectContent>("default.fx",
                                                                      oIdentity);

            // Copy the texture setting across from the original material.
            BasicMaterialContent basicMaterial = material as BasicMaterialContent;

            if ((basicMaterial != null) && (basicMaterial.Texture != null))
            {
                newMaterial.Textures.Add("Texture", basicMaterial.Texture);
            }


            return base.ConvertMaterial(newMaterial, context);
        }
        
    }
}